package com.lgkj.ui.manager;

import android.app.ActivityOptions;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.google.gson.Gson;
import com.lgkj.R;
import com.lgkj.adapter.HairSalonAdapter;
import com.lgkj.bean.HairSalonBean;
import com.lgkj.ui.BaseActivity;
import com.lgkj.ui.ImageBrowseActivity;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/6/25 0025.
 */

public class HairSalonActivity extends BaseActivity {
    @BindView(R.id.gv_item)
    GridView gvItem;
    private HairSalonAdapter adapter;
    private HairSalonBean dataBean;


    @Override
    public int getLayoutId() {
        return R.layout.activity_hair_salon;
    }

    @Override
    public void initView() {
        setTitle("发型屋");
        adapter = new HairSalonAdapter(HairSalonActivity.this);
        gvItem.setAdapter(adapter);
        itemclick();
    }

    @Override
    protected void onResume() {
        super.onResume();
        data();
    }

    public void initData() {

    }

    private void data() {
        showDiaLog("正在获取数据");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(HairSalonActivity.this));
        map.put("barbershop_id", CommonUtils.getShopId(HairSalonActivity.this));
        HttpUtils.postHttp(map, CommonConstant.hairList, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {
                    Gson mGson = new Gson();
                    dataBean = mGson.fromJson(String.valueOf(jsonObject), HairSalonBean.class);
                    if (dataBean != null) {
                        adapter.setData(dataBean.getData());
                    }
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }

    private void itemclick() {
        adapter.setClick(new HairSalonAdapter.MyClickListener() {
            @Override
            public void clickListener(View v) {
                if (v.getId() == R.id.iv_del) {
                    int id = Integer.parseInt(v.getTag().toString());
                    Log.i(TAG, "clickListener: " + adapter.getItem(id).toString());
                    dele(adapter.getItem(id).getId());
                }
            }
        });
        gvItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                if (i == (adapterView.getChildCount() - 1)) {
                    startActivity(new Intent(HairSalonActivity.this, HaiSalonAddActivity.class));
                    //设置layout在PopupWindow中显示的位置
                    //添加
                } else {
                    //放大
                    Intent intent = new Intent(HairSalonActivity.this, ImageBrowseActivity.class);
                    intent.putExtra("image_url", adapter.getItem(i).getHairstyle_img());
                    startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(HairSalonActivity.this, gvItem, "shareNames").toBundle());
                }
            }
        });
    }

    private void dele(String id) {
        showDiaLog("正在删除");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(HairSalonActivity.this));
        map.put("hair_id", id);
        HttpUtils.postHttp(map, CommonConstant.delhair, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                toastLong(jsonObject.optString("msg"),0);
                if (jsonObject.optInt("code") == 0) {
                    data();
                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }

}
